/**
 * 文件大小排序
 * @param a
 * @param b
 * @returns {number}
 */
export const fileSizeSort = (a, b) => {
    const sizeUnits = {"TB": 4, "GB": 3, "MB": 2, "KB": 1};
    const regex = /([0-9.]+)([A-Z]{2})/;
    const matchA = a.toUpperCase().match(regex);
    const matchB = b.toUpperCase().match(regex);
    const sizeA = parseFloat(matchA[1]) * Math.pow(1024, sizeUnits[matchA[2]]);
    const sizeB = parseFloat(matchB[1]) * Math.pow(1024, sizeUnits[matchB[2]]);

    if (sizeA > sizeB) return 1;
    if (sizeA < sizeB) return -1;
    return 0;
}

/**
 * 时间排序（目前是xx秒xx毫秒）
 * @param a
 * @param b
 */
export const timeSort = (a, b) =>{
    const regex = /(?:(?<seconds>\d+)秒)?(?:(?<milliseconds>\d+)毫秒)?/;
    const matchA = a.match(regex);
    const matchB = b.match(regex);
    const timeA = matchA ? (matchA.groups.seconds ? parseInt(matchA.groups.seconds, 10) : 0) * 1000 + (matchA.groups.milliseconds ? parseInt(matchA.groups.milliseconds, 10) : 0) : 0;
    const timeB = matchB ? (matchB.groups.seconds ? parseInt(matchB.groups.seconds, 10) : 0) * 1000 + (matchB.groups.milliseconds ? parseInt(matchB.groups.milliseconds, 10) : 0) : 0;

    if (timeA > timeB) return 1;
    if (timeA < timeB) return -1;
    return 0;
}
